package com.gxc.blog.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.gxc.blogm.domain.Media;
import com.gxc.blogm.service.MediaService;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.JsonUtil;
import com.gxc.webcore.util.RequestUtil;

public class MediaController {

	MediaService mediaService;

	public MediaService getMediaService() {
		return mediaService;
	}

	public void setMediaService(MediaService mediaService) {
		this.mediaService = mediaService;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView listByAlbum(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String album_id = request.getParameter("album_id");
		Map model = new HashMap();
		model.put("album_id", album_id);
		return new ModelAndView("blog/media/media_listByAlbym.vm", model);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView listPageByAlbum(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		String album_id = request.getParameter("album_id");
		Map params = new HashMap();
		params.put("album_id", album_id);
		PageList pageList = mediaService.listPage(params, pageInfo);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", pageList);
		String jsonResult = JsonUtil.object2json(result);
		response.getWriter().write(jsonResult);
		return null;
	}

	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("blog/media/media_listByAlbym.vm");
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView listPage(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		Map params = RequestUtil.getInMap(request);
		PageList pageList = mediaService.listPage(params, pageInfo);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", pageList);
		String jsonResult = JsonUtil.object2json(result);
		response.getWriter().write(jsonResult);
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Media media = mediaService.retrieve(id);
		Map params = new HashMap();
		params.put("album", media.getAlbum_id());
		List mediaList = mediaService.listAll(params);
		Map model = new HashMap();
		model.put("media", media);
		model.put("mediaList", mediaList);
		return new ModelAndView("blog/media/media_view.vm", model);
	}

}
